package com.ruoyi.jx.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.jx.domain.MemberBalanceRecord;
import com.ruoyi.jx.mapper.MemberBalanceRecordMapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jx.mapper.MemberMapper;
import com.ruoyi.jx.domain.Member;
import com.ruoyi.jx.service.IMemberService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员管理Service业务层处理
 * 
 * @author p
 * @date 2021-10-07
 */
@Service
@Transactional
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberBalanceRecordMapper memberBalanceRecordMapper;

    /**
     * 查询会员管理
     * 
     * @param id 会员管理ID
     * @return 会员管理
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询会员管理列表
     * 
     * @param member 会员管理
     * @return 会员管理
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增会员管理
     * 
     * @param member 会员管理
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员管理
     * 
     * @param member 会员管理
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员管理
     * 
     * @param ids 需要删除的会员管理ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] ids)
    {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除会员管理信息
     * 
     * @param id 会员管理ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper.deleteMemberById(id);
    }

    /**
     * 修改会员的 余额 (  充值/抵扣
     *
     * @param params
     * @return
     */
    @Override
    public AjaxResult updateMemeberBalance(Map<String, Object> params) {

        //  会员id
        Long  memberId = MapUtils.getLong(params,"id");

        // 根据 memberId  查询会员
        Member member = memberMapper.selectMemberById(memberId);
        if (null == member) {
            return AjaxResult.error("会员信息异常");
        }
        // 当前登录人
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        BigDecimal oldMoney = BigDecimal.ZERO;
        BigDecimal  thisMoeny = BigDecimal.ZERO;
        BigDecimal  afterMoeny = BigDecimal.ZERO;
        AjaxResult ajaxResult = AjaxResult.success("操作成功");
        AjaxResult ajaxResultError = AjaxResult.error("操作失败");

        if (null != member) {
            oldMoney = member.getMoney();
        }

        int re = 0;
        // 操作类型
        Integer type = MapUtils.getInteger(params,"type",0);


        if (null != memberId) {

            // 余额 变动
            switch (type){
                case 0:
                    // 充值
                    thisMoeny = new BigDecimal( MapUtils.getString(params,"moneyCZ","0"));
                    // 顺便增加 用户的金额吧
                    re = addMemeberBalance(memberId,thisMoeny, oldMoney);
                    afterMoeny = oldMoney.add(thisMoeny);
                    break;
                case 1:
                    // 抵扣金额
                    thisMoeny = new BigDecimal( MapUtils.getString(params,"moneyDK","0"));
                    if (thisMoeny.compareTo(oldMoney) > 0) {
                        // 说明什么
                        ajaxResultError.put("msg","抵扣金额超出余额");
                        return ajaxResultError;
                    }

                    re = subMemeberBalance(memberId,thisMoeny, oldMoney);
                    afterMoeny = oldMoney.subtract(thisMoeny);
                    break;
            }
            // 操作金额
            // MapUtils.getString(params,"moneyCZ",0);
            // 增加变动记录
            /*
              `type` int(11) DEFAULT '0' COMMENT '变动类型0充值1消费抵扣',
  `change_money` decimal(16,2) DEFAULT '0.00',
  `before_money` decimal(16,2) DEFAULT '0.00' COMMENT '变动之前的金额',
  `after_money` decimal(16,2) DEFAULT '0.00' COMMENT '变动之后的金额',
             */
            MemberBalanceRecord mbr = new MemberBalanceRecord();
            mbr.setType(Long.valueOf(type));
            mbr.setChangeMoney(thisMoeny);
            mbr.setBeforeMoney(oldMoney);
            mbr.setAfterMoney(afterMoeny);
            mbr.setCreateBy(sysUser.getUserName());
            re = memberBalanceRecordMapper.insertMemberBalanceRecord(mbr);

        }

        return re > 0 ? ajaxResult : ajaxResultError;
    }

    /**
     * 添加 余额
     *
     * @param money
     * @param oldMoney
     * @return
     */
    @Override
    public int addMemeberBalance(Long id, BigDecimal money, BigDecimal oldMoney) {


        return memberMapper.addMemeberBalance(id,money,oldMoney);
    }

    /**
     * 抵扣 余额
     *
     * @param money
     * @param oldMoney
     * @return
     */
    @Override
    public int subMemeberBalance(Long id, BigDecimal money, BigDecimal oldMoney) {
        return memberMapper.subMemeberBalance(id,money,oldMoney);
    }


    // end
}
